package com.youuvs.controller;

import com.youuvs.feign.StoreFeign;
import com.youuvs.domain.Product;
import com.youuvs.domain.Store;
import com.youuvs.dto.Dto;
import com.youuvs.jsonResult.JsonResult;
import com.youuvs.service.IProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/product")
public class ProductController {

    @Autowired
    private IProductService productService;

    @Autowired
    private StoreFeign storeFeign;

    @PostMapping("/insert")
    public JsonResult insert(@RequestBody Dto dto){

        try {
            Product product = new Product();
            product.setName(dto.getName());
            product.setPrice(dto.getPrice());
            productService.insert(product);
            Store store = new Store();
            store.setNumber(dto.getNumber());
            store.setProduct_id(product.getId());
            storeFeign.insert(store);
            return new JsonResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new JsonResult().setMsg("添加失败pro");
        }
    }

    @GetMapping("/getById/{id}")
    public Product getById(@PathVariable("id") Long id){
        return productService.getById(id);
    }


   /* public JsonResult stoIns(@RequestBody Store store){
        storeFeign.insert(store);
        return new JsonResult();
    }*/
}
